package com.voice.call.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maxmind.geoip2.model.CountryResponse;
import com.voice.call.entity.OpLog;
import com.voice.call.mapper.OpLogMapper;
import com.voice.call.service.IpQueryService;
import com.voice.call.service.OpLogService;
import com.voice.call.util.entity.SupportRegister;
import com.voice.call.utils.StringUtil;

@Service
public class OpLogServiceImpl implements OpLogService{
	
	@Resource
	private OpLogMapper opLogMapper;
	
	@Resource
	private IpQueryService ipQueryService;

	@Override
	public boolean addOpLog(OpLog entity){
		
		try {
			
			if (entity == null) {
				return false;
			}
			if (opLogMapper.insert(entity)>0) {
				return true;
			}
			return false;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	//登录日志
	@Override
	public	void loginWriteLog(SupportRegister supportRegister,String desc,String v,String ip){
		OpLog opLog = new OpLog();
		opLog.setType("login");
		if (StringUtil.isEmpty(supportRegister.getMcc())) {
			opLog.setError("根据ip获取区域失败");
		}else {
			opLog.setMcc(supportRegister.getMcc());
			opLog.setError("not support "+supportRegister.getCountry());
		}
		opLog.setV(v);
		opLog.setIp(ip);
		if (!StringUtil.isEmpty(desc)) {
			opLog.setDesc(desc);
		}
		opLog.setCtime(new Date());
		this.addOpLog(opLog);
	}
	//提示升级
	@Override
	public	void updateWriteLog(String v,String ip){
		OpLog opLog = new OpLog();
		opLog.setType("update");
		opLog.setV(v);
		opLog.setIp(ip);
		CountryResponse resp=ipQueryService.getIpFoResponse(ip);
		if (resp!=null) {
			opLog.setMcc(resp.getCountry().getIsoCode());
			opLog.setDesc(resp.getCountry().getName());
		}
		opLog.setCtime(new Date());
		this.addOpLog(opLog);
		
	}
	//拨打日志
	@Override
	public	void callWriteLog(Integer userId,String desc,String error,String v,String ip){
		OpLog opLog = new OpLog();
		opLog.setType("calls");
		if (userId!=null) {
			opLog.setUserId(userId);
		}
		if (!StringUtil.isEmpty(ip)) {
			CountryResponse resp=ipQueryService.getIpFoResponse(ip);
			if (resp!=null) {
				opLog.setMcc(resp.getCountry().getIsoCode());
			}
			opLog.setIp(ip);
		}
		if (!StringUtil.isEmpty(v)) {
			opLog.setV(v);
		}
		if (!StringUtil.isEmpty(desc)) {
			opLog.setDesc(desc);
		}
		if (!StringUtil.isEmpty(error)) {
			opLog.setError(error);
		}
		opLog.setCtime(new Date());
		this.addOpLog(opLog);
	}
	
	//老虎机
	@Override
	public	void luckyCreditWriteLog(Integer userId,String v,String ip){
		OpLog opLog = new OpLog();
		opLog.setType("luckyCredit");
		opLog.setUserId(userId);
		opLog.setDesc("play");
		opLog.setError("no ads");
		opLog.setV(v);
		if (!StringUtil.isEmpty(ip)) {
			CountryResponse resp=ipQueryService.getIpFoResponse(ip);
			if (resp!=null) {
				opLog.setMcc(resp.getCountry().getIsoCode());
			}
			opLog.setIp(ip);
		}
		opLog.setCtime(new Date());
		this.addOpLog(opLog);
	}
	//无效手机号
	@Override
	public	void phoneWriteLog(String phone,String v,String ip){
		OpLog opLog = new OpLog();
		opLog.setType("phone");
		opLog.setDesc(phone);
		opLog.setError("Invalid mobile number");
		opLog.setV(v);
		if (!StringUtil.isEmpty(ip)) {
			CountryResponse resp=ipQueryService.getIpFoResponse(ip);
			if (resp!=null) {
				opLog.setMcc(resp.getCountry().getIsoCode());
			}
			opLog.setIp(ip);
		}
		opLog.setCtime(new Date());
		this.addOpLog(opLog);
	}
	
	//是否为无用的手机号
	@Override
	public	boolean existsValidPhone(String phone){
		
		Map<String, Object> parmsMap=new HashMap<String, Object>();
		parmsMap.put("phone", phone);
		parmsMap.put("type", "phone");
		int total = opLogMapper.count(parmsMap);
		if (total>0) {
			return true;
		}
		return false;
	}
}
